<?php

namespace app\common\model;

use app\common\service\StatusService;
use think\Model;


class OrderAddress extends Model
{

    

    

    // 表名
    protected $name = 'order_address';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
	
	/**
	 * 添加订单收货地址
	 * @param $orderId
	 * @param $userId
	 * @param $addressId
	 * @return bool|false|int
	 * @throws \think\exception\DbException
	 */
	public function addRow($orderId, $userId, $addressId) {
		$address = Address::get(['user_id' => $userId, 'id' => $addressId, 'status' => StatusService::STATUS_NORMAL]);
		if(!$address) {
			$this->error = '收货地址不存在';
			return false;
		}
		
		$res = $this->isUpdate(false)->save([
			'order_id' => $orderId,
			'name' => $address['name'],
			'mobile' => $address['mobile'],
			'province_id' => $address['province_id'],
			'city_id' => $address['city_id'],
			'area_id' => $address['area_id'],
			'address' => $address['address']
		]);
		
		return $res;
	}
	
	
}
